﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class Projectile_DestroyState : State
    {
        private static Projectile_DestroyState instance;

        private Projectile_DestroyState()
        {
            base.Name = "DestroyState";
        }

        public override void enter(AIObject aiObject)
        {
            //Console.WriteLine("<Projectile_DestroyState> enter");

            // Change animation
           

            AIProjectile aiProjectile = (AIProjectile)aiObject;
            Projectile projectile = (Projectile)aiProjectile.Projectile;
            aiProjectile.Projectile.changeAnimation("explosion");
            EventEmitter.getInstance().registerMessage(1000000, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_remove);

            GameInfo gameInfo = GameInfo.getInstance();
            if (Game1.enableMusic)
                Game1.explosionProjectileSound.Play();

            // Send message victim
            Object victim = gameInfo.getObject(projectile.IdVictim);
            if (victim != null)
            {
                if (victim.Type == Object.TypeObject.colision)
                {
                    gameInfo.receivedAttack(victim, 20);
                }
                else
                {
                    if (victim.Type != Object.TypeObject.projectile)
                    {
                        if (projectile.Team != victim.Team || victim.FireFriendly == true)
                        {
                            Object emisor = gameInfo.getObject(projectile.IdEmissor);
                            if (emisor != null && victim.Blinking == false)
                                emisor.Kills = emisor.Kills + 1;

                            EventEmitter.getInstance().registerMessage(0, victim.Id, victim.Id, Message.MessageType.msg_attacked);
                        }
                    }
                }
            }
   
        }

        public static Projectile_DestroyState getInstance()
        {
            if (instance == null)
                instance = new Projectile_DestroyState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {

            /*
            AIProjectile aiProjectile = (AIProjectile)aiObject;
            if (aiProjectile.Projectile.Movement == null)
            {
                //Emisor->enviarMensaje(MENSAJE_INMEDIATO, proyectil->getId(), proyectil->getId(), msg_Impactado, SIN_INFORMACION);
                // Delete object
                GameInfo.getInstance().removeObject(aiProjectile.Id, aiProjectile.Projectile.Type);
            }
            else
            {
                // Check colision
                GameInfo gameInfo = GameInfo.getInstance();
                if (gameInfo.checkColision(aiProjectile.Projectile))
                {
                    System.Console.WriteLine("colision detected");
                    
                }
                //for(int i = 0; i < gameInfo.
            }*/
        }

        public override void exit(AIObject aiObject)
        {
            //Console.WriteLine("<Projectile_DestroyState> exit");
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            //Console.WriteLine("<Projectile_DestroyState> onMessage " + message.Type);
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_destroy:
                    {
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_remove:
                    {

                        GameInfo.getInstance().removeObject(aiObject.Id, Object.TypeObject.projectile);
                        correct = true;
                        break;
                    }
            }
            return correct;
        }

    }
}
